const API_BASE = 'https://tihaidengta.xyz';
// const API_BASE = 'http://172.20.10.14:8001';
// const { checkLoginAndActivation } = require('../../../utils/auth.js'); 
const { request } = require('../../../utils/http');
// exam.js
Page({
  data: {
    bankId: '',
    bankName: '',
    chapters: [],    // 章节数据列表
    bookList: []  // 初始化

  },

  onLoad(options) {
    const bankId = options.bankId;
    const bankName = decodeURIComponent(options.bankName || '');
    this.setData({ bankId, bankName });
      
    this.fetchChapters(bankId);

    // 可在此处请求 bankId 对应的章节列表
  },
  fetchChapters(bankId) {
    // console.log('📥 fetchChapters 请求 URL:', `${API_BASE}/eduservice/subjectChapter/allChapter/${bankId}`);
    request({
      url: `${API_BASE}/eduservice/subjectChapter/allChapter/${bankId}`,
      method: 'GET'
    })
    .then(res => {
      // console.log('📥 fetchChapters 响应 raw:', res);
      // 根据你的 http.js，res 已经是 res.data
      if (res.code === 20000 && Array.isArray(res.data.list)) {
        this.setData({
          chapters: res.data.list
        });
        // console.log('✅ chapters 已设置到 data:', this.data.chapters);
      } else {
        // console.error('❌ fetchChapters 返回异常 code:', res.code, 'message:', res.message);
        wx.showToast({ title: '激活码失效,请先激活当前页面', icon: 'none' });
      }
    })
    .catch(err => {
      console.error('❌ fetchChapters 请求出错:', err);
      wx.showToast({ title: '请求失败，请检查网络', icon: 'none' });
    });
  },

   // 示例跳转章节详情（你可自定义）
   goToChapterDetail(e) {
    const item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: `/subpackage/practice/chapterPractice/chapterPractice?chapterCode=${item.chapterCode}&bankId=${this.data.bankId}&source=chapter`
    });
  },
  // goToChapterDetail(e) {
  //   const item = e.currentTarget.dataset.item;
  //   checkLoginAndActivation()
  //     .then(() => {
  //       // 登录+激活没问题后跳转
  //       wx.navigateTo({
  //         url: `/pages/chapterPractice/chapterPractice?chapterCode=${item.chapterCode}&bankId=${this.data.bankId}&source=chapter`
  //       });
  //     })
  //     .catch(err => {
  //       console.log('拦截跳转:', err);
  //     });
  // },
  // 题库列表点击某一本书时
goToExam(e) {
  const bankId = e.currentTarget.dataset.id;
  wx.navigateTo({
    url: `/subpackage/exam/exam/exam?bankId=${bankId}`
  });
}
});